package ext.tianma.util;

import java.util.List;

import ext.tianma.workflow.BomWorkFlowUtil;
import wt.fc.ObjectReference;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.ReferenceFactory;
import wt.fc.WTObject;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.project.Role;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.team.Team;
import wt.team.TeamManaged;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.workflow.engine.WfProcess;

public class UserUtil {

	private static String internal_value = "RECIPIENT";
	private static String name = "B110691";

	/**
	 * 给角色添加用户
	 * 
	 * @param self
	 *            obj
	 * @param internal_value
	 *            角色名
	 * @param name
	 *            用户名
	 * @throws WTPropertyVetoException
	 * @throws WTException
	 */
	public static void addPrincipalToRole(WTObject primaryBusinessObject, ObjectReference self)
			throws WTPropertyVetoException, WTException {

		if (primaryBusinessObject instanceof WTPart) {
			WTPart part = (WTPart) primaryBusinessObject;
			String viewName = part.getViewName();
			System.out.println("viewName====>>>" + viewName);
			boolean ben = checkEffectiveSite(part);
			System.out.println("viewName====>>>" + ben);
			if (ben) {

				WfProcess process = BomWorkFlowUtil.getProcess(self);
				wt.team.Team team = null;
				try {
					team = getTeam(process);
				} catch (Exception ex) {
					ex.printStackTrace();
					throw new WTException(ex);
				}

				Role theRole = Role.toRole(internal_value);
				WTUser user = getUserByName(name);
				System.out.println("theRole===" + theRole + "==>>user===>>" + user);
				if (user == null) {
					throw new WTException("没有找到用户: [" + user + "] ");
				} else {
					team.addPrincipal(theRole, user);
				}
				PersistenceHelper.manager.refresh(team);

			}

		}
	}

	// 根据用户名查询WTUser对象
	public static WTUser getUserByName(String username) throws WTException {
		WTUser user = null;
		QuerySpec qs = new QuerySpec(WTUser.class);
		SearchCondition sc = new SearchCondition(WTUser.class, WTUser.NAME, "=", username);
		qs.appendWhere(sc);
		QueryResult qr = PersistenceHelper.manager.find(qs);
		while (qr.hasMoreElements()) {
			user = (WTUser) qr.nextElement();
		}
		return user;
	}

	/**
	 * 根据WfProcess对象，获得团队
	 * 
	 * @param process
	 * @return
	 * @throws Exception
	 */
	public static Team getTeam(WfProcess process) throws Exception {

		Team team = null;
		ReferenceFactory rf = new ReferenceFactory();
		String oid = rf.getReferenceString(process);
		WTObject wtobject = (WTObject) rf.getReference(oid).getObject();
		if (wtobject instanceof TeamManaged) {
			team = (Team) ((TeamManaged) wtobject).getTeamId().getObject();
		} else if (wtobject instanceof Team) {
			team = (Team) wtobject;
		}
		return team;
	}

	/**
	 * 判定料号生效地是否包含深圳
	 * 
	 * @param part
	 * @return boolean
	 */
	private static boolean checkEffectiveSite(WTPart part) {
		List<String> effectiveSite = IbaAttributeUtil.getAttributeValues(part, "partNumberEffectiveSite");
		boolean temp = false;
		System.out.println("effectiveSite===>>" + effectiveSite);
		for (String site : effectiveSite) {
			if (site.contains(TianMaConstants.EffectiveSite.K10.getSite())) {
				temp = true;
				break;
			}
		}
		return temp;
	}

}
